package com.demo.dao.mapper;

import com.demo.model.Order;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class OrderMapper {
    public String insert(Order order) {
        return "insert into `order` (userId, total, payStatus) values (#{userId},#{total},#{payStatus})";
    }

    public String delete(Integer id) {
        BEGIN();
        DELETE_FROM("`order`");
        WHERE("id=#{id}");
        return SQL();
    }

    public String update(Order order) {
        BEGIN();
        UPDATE("`order`");
        if (order.getUserId() != null && order.getUserId() > 0) {
            SET("userId=#{userId}");
        }
        if (order.getTotal() != null && order.getTotal() > 0) {
            SET("total=#{total}");
        }
        if (order.getPayStatus() != null && order.getPayStatus() > 0) {
            SET("payStatus=#{payStatus}");
        }
        WHERE("id=#{id}");
        String sql = SQL();
        System.out.println(sql);
        return sql;
    }

    public String select(Integer userId) {
        if (userId == null) {
            return "select * from `order` order by id desc";
        } else {
            return "select * from `order` where userId=#{userId} order by id desc";
        }
    }

    public String selectOne(Integer id) {
        return "select * from `order` where id=#{id}";
    }

    public String selectOneByUserId(Integer userId) {
        return "select * from `order` where userId=#{userId}";
    }
}
